﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.ComponentModel;
using System.Web;
using Skynet.BusinessLogic.BusinessComponents;

namespace Skynet.WebAdministracion.Views.StartExamViewer
{
    [System.ComponentModel.DataObject]
    public class StartExamViewerPresenter : Infrastructure.Web.Views.Master.MasterPresenter<IStartExamViewerView>
    {
        [Dependency]
        public WebFoundation.Services.Session.ISessionService SessionService { get; set; }

        [Dependency]
        public WebFoundation.Services.Navigation.INavigationService NavigationService { get; set; }


        
        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExamQueries BCExamQueries { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExams BCExams { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCCustomers BCCustomers { get; set; }

        [Dependency]
        public IBCCustomersQueries BCCustomersQueries { get; set; }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public object GetActiveExams(Guid ExamByCustomerId)
        {
            var userName = HttpContext.Current.User.Identity.Name;
            var customer = this.BCCustomersQueries.FindCustomers(option: BusinessLogic.BusinessComponents.CustomerFindOptions.GetByUserName, UserName: userName).SingleOrDefault();
            var exams = this.BCExamQueries.SearchExamByCustomer(options: BusinessLogic.BusinessComponents.ExamByCustomerSearchOptions.RuntimeByCustomerId, CustomerId: customer.Id);
            var query = (from c in exams where c.Id == ExamByCustomerId select new { Id = c.Id, Category = c.Exam.ExamCategory.Title, Title = c.Exam.Title, Duration = c.Duration, ValidFrom = c.ValidFrom, ValidTo = c.ValidTo }).SingleOrDefault();
            return query;
        }

        public void StartExam(Guid key)
        {
            this.BCExams.AcceptExam(key);
            this.SessionService.RuntimeExamByCustomerId = key;            
            this.SessionService.RuntimeExamQuestions = new LinkedList<BusinessEntities.BaseQuestion>(this.BCExamQueries.SearchQuestion( QuestionSearchOptions.RuntimeByCustomerExamId, CustomerExamId:key));
            this.SessionService.RuntimeExamQuestionPosition = 0;
            this.NavigationService.Navigate("~/Evaluation/ExamViewer.aspx");
        }
    }
}
